package com.demo.fragment;

import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.demo.domain.Contact;
import com.demo.R;

/**
 * Contact preview fragment. Display contact info.
 * Author: Alexey Tokan
 * Date: 26.07.12
 * Time: 10:56
 */
public class PreviewForm extends Fragment {

    private TextView firstNameView;
    private TextView lastNameView;
    private TextView phoneView;
    private TextView emailView;
    private ImageView photoView;

    private Contact contact;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.preview_form, container, false);

        firstNameView = (TextView) view.findViewById(R.id.first_name_preview);
        lastNameView = (TextView) view.findViewById(R.id.last_name_preview);
        phoneView = (TextView) view.findViewById(R.id.phone_preview);
        emailView = (TextView) view.findViewById(R.id.email_preview);
        photoView = (ImageView) view.findViewById(R.id.photo_preview);

        if (bundle != null || (bundle = getArguments()) != null
                || (bundle = getActivity().getIntent().getExtras()) != null) {

            contact = (Contact) bundle.get(Contact.CONTACT);

            fillFields(contact);

        }

        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(Contact.CONTACT, contact);
    }

    /**
     * Fill fields with contact data
     * @param contact contact which data with which fragment fields would be filled.
     */
    public void fillFields(Contact contact) {
        firstNameView.setText(contact.getFirstName());
        lastNameView.setText(contact.getLastName());
        phoneView.setText(contact.getPhone());
        emailView.setText(contact.getEmail());
        if (contact.getPhoto() != null) {
            photoView.setImageURI(Uri.parse(contact.getPhoto()));
        }
    }

}
